package com.mangofactory.aop.interceptors
{
	import com.mangofactory.aop.configuration.ClassProxyDefinition;
	
	import org.as3commons.bytecode.interception.IInterceptor;
	import org.as3commons.bytecode.interception.IMethodInvocation;
	import org.as3commons.bytecode.interception.IMethodInvocationInterceptor;
	import org.as3commons.bytecode.interception.InvocationKind;
	import org.as3commons.bytecode.template.MethodInvocation;
	
	/**
	 * This interceptor gets invoked for all methods which are
	 * proxied on the target class.
	 * It in turn resolves the target to the specific interceptor for
	 * the given method (normally 
	 * an AspectInvokingInterceptor), and invokes it.*/
	public class DelegatingInterceptor implements IInterceptor
	{
		private var proxyDefinition:ClassProxyDefinition;
		public function DelegatingInterceptor(proxyDefinition:ClassProxyDefinition)
		{
			this.proxyDefinition = proxyDefinition;
		}
		
		public function intercept(invocation:IMethodInvocation):void
		{
			if(invocation.kind == InvocationKind.METHOD)
			{
				var interceptor:IInterceptor = proxyDefinition.getInterceptor(invocation.targetMember.localName);
				interceptor.intercept(invocation);
			}
		}
	}
}